﻿<%@ WebHandler Language="C#" Class="async" %>

using System;
using System.Web;

public class async : IHttpAsyncHandler {
    static log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
    public void ProcessRequest (HttpContext context) {
    }
    
    public AsyncResult r;
    public string content;
 
    public bool IsReusable {
        get {
            return false;
        }
    }


    #region IHttpAsyncHandler Members

    public IAsyncResult BeginProcessRequest(HttpContext context, AsyncCallback cb, object extraData)
    {
        string url;
        url = ini.LongTaskUrl;

        System.Net.WebClient c = new System.Net.WebClient();
        c.DownloadStringCompleted += new System.Net.DownloadStringCompletedEventHandler(c_DownloadStringCompleted);
        c.DownloadStringAsync(new Uri(url));
        this.r = new AsyncResult(c, cb, context);
        log.Debug("Start");
        return r;
    }

    void c_DownloadStringCompleted(object sender, System.Net.DownloadStringCompletedEventArgs e)
    {
        content = e.Result;
        log.Debug("Process");
        this.r.cb(this.r);
    }

    public void EndProcessRequest(IAsyncResult result)
    {
        log.Debug("End");
        r.context.Response.Write(content);
    }

    #endregion
}